import Vue from 'vue'
import Router from 'vue-router'
import api from '../api/routes.js'
import Vuex from 'vuex'
import layout from '@/layout'
import store from '../store'
import  roleApi from '@/api/system/menu.js'
import {formatRoutes} from '../utils/parsing'
import {getAsyncRoutes} from '../utils/asyncRouter.js'
import NProgress from 'nprogress' // progress bar
import { getToken } from '@/utils/auth' // get token from cookie
Vue.use(Router)
// 配置项目中没有涉及权限的公共路由
export const constantRoutes = [
    {
        path: '/login',
        component:resolve => require(['@/views/login'],resolve),
        hidden: true
    },
	{
	    path: '/',
	    component: resolve => require(['@/views/login'],resolve),
	    hidden: true
	},
    {
        path: '/404',
        component:resolve => require(['@/views/404'],resolve), 
        hidden: true
    },
    {
        path: '/association/:pageId',
		props: true, 
        component: resolve => require(['@/views/association/index'],resolve),
    },
    {
        path: '/survey/:projectId/:pageId',
		props: true, 
        component:resolve => require(['@/views/association/survey'],resolve),
    },
    {
        path: '/finish/:pageId',
		props: true, 
        component:resolve => require(['@/views/association/finish'],resolve),
    },
    {
        path: '/generator/:pageId',
		props: true, 
        component:resolve => require(['@/views/main/fitment/generator'],resolve),
    }
]
// roleApi.list().then(res=>{
// 	localStorage.setItem("menus",JSON.stringify(formatRoutes(res.menuInfo)))
// })
// const menusAll = JSON.parse(localStorage.getItem("menus"))

// constantRoutes.push({
// 	path:'/admin',
// 	redirect: '/admin/home',
// 	component:resolve => require([`@/layout/index`],resolve),
// 	children:getAsyncRoutes(menusAll)
// }) 
roleApi.list().then(res=>{
	localStorage.setItem("menus",JSON.stringify(formatRoutes(res.menuInfo)))
	
	var data = routesData(res.menuInfo)
	data.forEach(item =>{
		router.addRoute(item)
	})
})	

function routesData(result){
	constantRoutes.push({
	path:'/main',
	redirect: '/main/home',
	component:resolve => require([`@/layout/index`],resolve),
	children:getAsyncRoutes(result)
	})
	return constantRoutes
}



const createRouter = () => new Router({
    //mode: 'history',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()


const whiteList = ['/login','/survey','/finish'];
	
router.beforeEach( async (to, from, next) => {
    // 获取用户token，用来判断当前用户是否登录
    const hasToken = getToken()
    if (hasToken) {
        if (to.path === '/login'|to.path === '/') {
            next({ path: '/main/home' })
        } else {
            next()
        }
    } else {
        if (whiteList.indexOf(to.path.replace(/(\W\d)/ig, ``)) !== -1) {
           next()
        } else {
            next(`/login?redirect=${to.path}`)
            // NProgress.done()
        }
    }
})


router.afterEach(() => {
    NProgress.done()
})


export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router